package com.tang.process;

import com.tang.bean.WaterSensor;
import com.tang.functions.WaterSensorMapFunction;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

/**
 * 侧输出流demo
 * 当水位超过10的时候，就测流输出一个告警
 *
 * @author tang
 * @since 2023/7/10 18:10
 */
public class SideOutputDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());

        SingleOutputStreamOperator<WaterSensor> dataStream = environment.socketTextStream("192.168.70.141", 7777)
                // 将输入数据转换成类
                .map(new WaterSensorMapFunction())
                // 指定水位线和时间戳
                .assignTimestampsAndWatermarks(
                        // 乱序流的水位线，允许3s的迟到数据
                        WatermarkStrategy.<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                                    /**
                                     * 指定水位
                                     *
                                     * @param element The element that the timestamp will be assigned to.
                                     * @param recordTimestamp The current internal timestamp of the element, or a negative value, if
                                     *     no timestamp has been assigned yet. 元素当前的内置时间戳，或者一个负数，当未分配的时候
                                     * @return 水位
                                     */
                                    @Override
                                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                                        return element.getTs() * 1000L;
                                    }
                                })
                );

        OutputTag<String> warnTag = new OutputTag<>("warn", Types.STRING);
        SingleOutputStreamOperator<WaterSensor> process = dataStream.keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, WaterSensor>() {
                    @Override
                    public void processElement(WaterSensor value, KeyedProcessFunction<String, WaterSensor, WaterSensor>.Context ctx, Collector<WaterSensor> out) {
                        if (value.getVc() > 10) {
                            ctx.output(warnTag, "当前水位线 = " + value.getVc() + ",大于阈值10！！！");
                        }
                        // 主流正常运行
                        out.collect(value);
                    }
                });
        process.print("主流正常输出");
        process.getSideOutput(warnTag).printToErr("测流输出");

        environment.execute();
    }

}
